;;; Modifications by Anders Weinstein 2002-2008
;;; Modifications by Brett van de Sande, 2005-2008
;;; Copyright 2009 by Kurt Vanlehn and Brett van de Sande
;;;  This file is part of the Andes Intelligent Tutor Stystem.
;;;
;;;  The Andes Intelligent Tutor System is free software: you can redistribute
;;;  it and/or modify it under the terms of the GNU Lesser General Public 
;;;  License as published by the Free Software Foundation, either version 3 
;;;  of the License, or (at your option) any later version.
;;;
;;;  The Andes Solver is distributed in the hope that it will be useful,
;;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;  GNU Lesser General Public License for more details.
;;;
;;;  You should have received a copy of the GNU Lesser General Public License
;;;  along with the Andes Intelligent Tutor System.  If not, see 
;;;  <http:;;;www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;;       list of PSMclasses grouped into catagories
;;;;   

;; Use (principles-file) to generate file KB/principles.tsv

(defparameter *principle-tree* '(
(group "Write a Principle"
(group "Kinematics" 
(group "Translational"			
       (leaf sdd :tutorial "Average speed")
       (leaf avg-velocity :bindings ((?axis . x)) :tutorial "Average velocity")
       (leaf avg-velocity :bindings ((?axis . y)) :tutorial "Average velocity")
       ;; alternative form of equation
       (leaf lk-no-s :EqnFormat ("a(avg)_~a = (vf_~a - vi_~a)/t" 
				 (axis-name ?axis) (axis-name ?axis) 
				 (axis-name ?axis))
	     :bindings ((?axis . x)) :tutorial "Average acceleration")
       ;; alternative form of equation
       (leaf lk-no-s :EqnFormat ("a(avg)_~a = (vf_~a - vi_~a)/t" 
				 (axis-name ?axis) (axis-name ?axis) 
				 (axis-name ?axis))
	     :bindings ((?axis . y)) :tutorial "Average acceleration")
       (leaf lk-no-vf :bindings ((?axis . x)) :tutorial "Constant acceleration")
       (leaf lk-no-vf :bindings ((?axis . y)) :tutorial "Constant acceleration")
       (leaf lk-no-s :bindings ((?axis . x)) :tutorial "Constant acceleration")
       (leaf lk-no-s :bindings ((?axis . y)) :tutorial "Constant acceleration")
       (leaf lk-no-t :bindings ((?axis . x)) :tutorial "Constant acceleration")
       (leaf lk-no-t :bindings ((?axis . y)) :tutorial "Constant acceleration")
       (leaf sdd-constvel :bindings ((?axis . x)) :tutorial "Constant velocity component")
       (leaf sdd-constvel :bindings ((?axis . y)) :tutorial "Constant velocity component")
       (leaf const-v :bindings ((?axis . x)) :tutorial "Constant velocity component")
       (leaf const-v :bindings ((?axis . y)) :tutorial "Constant velocity component")
       (leaf centripetal-accel :tutorial "Centripetal acceleration")
       (leaf centripetal-accel-compo :bindings ((?axis . x)) :tutorial "Centripetal acceleration")
       (leaf centripetal-accel-compo :bindings ((?axis . y)) :tutorial "Centripetal acceleration")
       (leaf relative-vel :bindings ((?axis . x)) :tutorial "Relative Velocity")
       (leaf relative-vel :bindings ((?axis . y)) :tutorial "Relative Velocity")
       )
(group "Rotational" 
       (leaf ang-sdd :tutorial "Angular velocity")
       ;; alternative form of equation
       (leaf rk-no-s :EqnFormat "$a(avg) = ($wf - $wi)/t" 
	     :tutorial "Angular acceleration")
       (leaf rk-no-vf :tutorial "Constant angular acceleration")
       (leaf rk-no-s :tutorial "Constant angular acceleration")
       (leaf rk-no-t :tutorial "Constant angular acceleration")
       (leaf linear-vel :tutorial "Linear velocity")
       (leaf rolling-vel :tutorial "Linear velocity")
       )
)
(group "Newton's Laws" 
       (group "Translational" 
	      (leaf NSL :bindings ((?axis . x)) :tutorial "Newton's Second Law")
	      (leaf NSL :bindings ((?axis . y)) :tutorial "Newton's Second Law")
	      (leaf net-force :bindings ((?axis . x)) :tutorial "Newton's Second Law")
	      (leaf net-force :bindings ((?axis . y)) :tutorial "Newton's Second Law")
	      (leaf NTL :tutorial "Newton's Third Law")
	      (leaf NTL-vector :bindings ((?axis . x)) :tutorial "Newton's Third Law")
		 (leaf NTL-vector :bindings ((?axis . y)) :tutorial "Newton's Third Law")
		 (leaf ug :tutorial "Universal Gravitation")
		 )
	 (group "Rotational" 
		 (leaf NFL-rot :tutorial "Newton's Law for rotation")
		 (leaf NSL-rot :tutorial "Newton's Law for rotation")
		 (leaf mag-torque :tutorial "Individual torque magnitude")
		 (leaf torque :bindings ((?xyz . x)) :tutorial "Individual torque magnitude")
		 (leaf torque :bindings ((?xyz . y)) :tutorial "Individual torque magnitude")
		 (leaf torque :bindings ((?xyz . z)) :tutorial "Individual torque magnitude")
		 (leaf net-torque-zc :tutorial "Net torque")
		 )
	 )
 (group "Work Energy and Power" 
	 (leaf work :tutorial "Work done by a force")
	 (leaf net-work :tutorial "Net work")
	 (leaf work-nc :tutorial "Conservation of Energy")
	 (leaf work-energy :tutorial "Work-Energy")
	 (leaf mechanical-energy :tutorial "Conservation of Energy")
	 (leaf change-me :tutorial "Conservation of Energy")
	 (leaf cons-energy :tutorial "Conservation of Energy")
	 (leaf potential-energy-definition)
	 (leaf kinetic-energy :tutorial "Conservation of Energy")
	 (leaf rotational-energy :tutorial "Conservation of Energy")
	 (leaf grav-energy :tutorial "Conservation of Energy")
	 (leaf gravitational-energy-point)
	 (leaf spring-energy :tutorial "Conservation of Energy")
	 (leaf electric-energy :tutorial "Electric Potential")
	 (leaf power :tutorial "Power")
	 (leaf net-power :tutorial "Power")
	 (leaf inst-power :tutorial "Power")
	 (leaf spherical-intensity-to-power :tutorial "Intensity")
	 (leaf uniform-intensity-to-power :tutorial "Intensity")
	 (leaf net-intensity :tutorial "Intensity")
	 (leaf intensity-to-decibels :tutorial "Intensity")
	 (leaf intensity-to-decibels :EqnFormat "I = Iref*10^($b/10)" :tutorial "Intensity")
	 (leaf intensity-to-poynting-vector-magnitude :tutorial "Intensity")
	 )
 (group "Momentum and Impulse" 
	 (group "Translational" 
		 (leaf momentum-compo :bindings ((?axis . x)) :tutorial "Conservation of Momentum")
		 (leaf momentum-compo :bindings ((?axis . y)) :tutorial "Conservation of Momentum")
		 (leaf cons-linmom :bindings ((?axis . x)) :tutorial "Conservation of Momentum")
		 (leaf cons-linmom :bindings ((?axis . y)) :tutorial "Conservation of Momentum")
		 (leaf cons-ke-elastic :tutorial "Elastic collisions")
		 (leaf impulse-force :bindings ((?axis . x)) :tutorial "Impulse")
		 (leaf impulse-force :bindings ((?axis . y)) :tutorial "Impulse")
		 (leaf impulse-momentum :bindings ((?axis . x)) :tutorial "Impulse")
		 (leaf impulse-momentum :bindings ((?axis . y)) :tutorial "Impulse")
		 (leaf NTL-impulse :tutorial "Impulse")
		 (leaf NTL-impulse-vector :bindings ((?axis . x)) :tutorial "Impulse")
		 (leaf NTL-impulse-vector :bindings ((?axis . y)) :tutorial "Impulse")
		 (leaf center-of-mass-compo :bindings ((?axis . x)) :tutorial "Center of Mass")
		 (leaf center-of-mass-compo :bindings ((?axis . y)) :tutorial "Center of Mass")
		 )
	 (group "Rotational" 
		 (leaf ang-momentum :tutorial "Angular momentum definition")
		 (leaf cons-angmom :tutorial "Conservation of Angular momentum")
		 )
	 )
 (group "Fluids"
	 (leaf pressure-height-fluid :tutorial "Pressure Height")
	 (leaf bernoulli :tutorial "Bernoulli's Principle")
	 (leaf equation-of-continuity :tutorial "Bernoulli's Principle")
	 (leaf pressure-force :tutorial "Pressure")
	 (leaf density :tutorial "Mass Density")
	 (leaf archimedes :tutorial "Buoyant Force")
	 )
 (group "Electricity and Magnetism"
         (leaf charged-particles) 
	 (leaf coulomb)
	 (leaf coulomb-compo :bindings ((?axis . x)))
	 (leaf coulomb-compo :bindings ((?axis . y)))
	 (leaf charge-force-Efield-mag :tutorial "Electric Field")
	 (leaf charge-force-Efield :bindings ((?axis . x)) :tutorial "Electric Field")
	 (leaf charge-force-Efield :bindings ((?axis . y)) :tutorial "Electric Field")
	 (leaf point-charge-Efield-mag :tutorial "Point Charge Field")
	 (leaf point-charge-Efield :bindings ((?axis . x)) :tutorial "Point Charge Field")
	 (leaf point-charge-Efield :bindings ((?axis . y)) :tutorial "Point Charge Field")
	 (leaf net-field-electric :bindings ((?axis . x)) :tutorial "Electric Field")
	 (leaf net-field-electric :bindings ((?axis . y)) :tutorial "Electric Field")
	 (leaf electric-flux-constant-field)
	 (leaf electric-flux-constant-field-change)
	 (leaf sum-fluxes)
	 (leaf point-charge-potential :tutorial "Electric Potential")
	 (leaf net-potential :tutorial "Electric Potential")
	 (leaf electric-energy :tutorial "Electric Potential")
	 (leaf gauss-law)
	 (leaf electric-dipole-moment-mag :tutorial "Dipoles")
	 (leaf electric-dipole-moment :bindings ((?axis . x)) :tutorial "Dipoles")
	 (leaf electric-dipole-moment :bindings ((?axis . y)) :tutorial "Dipoles")
	 (leaf electric-dipole-torque-mag :tutorial "Dipoles")
	 (leaf electric-dipole-torque :bindings ((?axis . z)) :tutorial "Dipoles")
	 (leaf electric-dipole-energy :tutorial "Dipoles")
	 (leaf charge-force-Bfield-mag :tutorial "Magnetic Field")
	 (leaf charge-force-Bfield :bindings ((?axis . x)) 
	       :tutorial "Magnetic Field")
	 (leaf charge-force-Bfield :bindings ((?axis . y)) 
	       :tutorial "Magnetic Field")
	 (leaf charge-force-Bfield :bindings ((?axis . z)) 
	       :tutorial "Magnetic Field")
	 (leaf current-force-Bfield-mag)
	 (leaf biot-savert-point-particle-mag)
	 (leaf biot-savert-point-particle :bindings ((?axis . x)))
	 (leaf biot-savert-point-particle :bindings ((?axis . y)))
	 (leaf biot-savert-point-particle :bindings ((?axis . z)))
	 (leaf straight-wire-Bfield)
	 (leaf center-coil-Bfield)
	 (leaf inside-solenoid-Bfield)
	 (leaf net-field-magnetic :bindings ((?axis . x)))
	 (leaf net-field-magnetic :bindings ((?axis . y)))
	 (leaf net-field-magnetic :bindings ((?axis . z)))
	 (leaf magnetic-flux-constant-field)
	 (leaf magnetic-flux-constant-field-change)
	 (leaf faradays-law)
	 (leaf amperes-law :tutorial "Ampere's Law")
	 (leaf magnetic-dipole-moment-mag :tutorial "Dipoles")
	 (leaf magnetic-dipole-moment :bindings ((?axis . x)) :tutorial "Dipoles")
	 (leaf magnetic-dipole-moment :bindings ((?axis . y)) :tutorial "Dipoles")
	 (leaf magnetic-dipole-moment :bindings ((?axis . z)) :tutorial "Dipoles")
	 (leaf magnetic-dipole-torque-mag :tutorial "Dipoles")
	 (leaf magnetic-dipole-torque :bindings ((?axis . x)) :tutorial "Dipoles")
	 (leaf magnetic-dipole-torque :bindings ((?axis . y)) :tutorial "Dipoles")
	 (leaf magnetic-dipole-torque :bindings ((?axis . z)) :tutorial "Dipoles")
	 (leaf magnetic-dipole-energy :tutorial "Dipoles")
	 (leaf electromagnetic-wave-field-amplitude)
	 )
 (group "Circuits" 
	 (leaf loop-rule :tutorial "Loop Rule")
	 (leaf junction-rule :tutorial "Junction Rule")
	 (leaf electric-power :tutorial "Electric Power")
	 (group "Resistance" 
		 (leaf equiv-resistance-series :tutorial "Series Resistors")
		 (leaf equiv-resistance-parallel :tutorial "Parallel Resistors")
		 (leaf ohms-law :tutorial "Ohm's Law")
		 )
	 (group "Capacitance" 
		 (leaf capacitance-definition :tutorial "Capacitance")
		 (leaf equiv-capacitance-parallel :tutorial "Parallel Capacitors")
		 (leaf equiv-capacitance-series :tutorial "Series Capacitors")
		 (leaf charge-same-caps-in-branch :tutorial "Series Capacitors")
		 (leaf junction-rule-cap)
		 (leaf cap-energy :tutorial "Capacitor Energy")
		 (leaf RC-time-constant :tutorial "RC Circuits")
		 (leaf charging-capacitor-at-time :tutorial "RC Circuits")
		 (leaf discharging-capacitor-at-time :tutorial "RC Circuits")
		 (leaf current-in-RC-at-time :tutorial "RC Circuits")
		 (leaf charge-capacitor-percent-max :tutorial "RC Circuits")
		 )
	 (group "Inductance" 
		 (leaf inductor-emf :tutorial "Inductance")
		 (leaf mutual-inductor-emf :tutorial "Inductance")
		 (leaf inductor-energy :tutorial "Inductance")
		 (leaf solenoid-self-inductance :tutorial "Inductance")
		 (leaf LR-time-constant :tutorial "LR Circuits")
		 (leaf LR-current-growth :tutorial "LR Circuits")
		 (leaf LR-growth-Imax :tutorial "LR Circuits")
		 (leaf LR-current-decay :tutorial "LR Circuits")
		 (leaf LR-decay-Imax :tutorial "LR Circuits")
		 (leaf LC-angular-frequency :tutorial "LC Circuits")
		 (leaf RLC-time-constant :tutorial "RLC Circuits")
		 (leaf RLC-angular-frequency :tutorial "RLC Circuits")
		 (leaf transformer-voltage)
		 (leaf transformer-power)
		 )
	 )
 (group "Optics" 
	 (leaf lens-eqn :tutorial "Lens Equation")
	 (leaf magnification-eqn :tutorial "Magnification")
	 (leaf focal-length-mirror :tutorial "Spherical Mirror")
	 (leaf lens-combo :tutorial "Combined Lenses")
	 (leaf combo-magnification :tutorial "Magnification")
	 (leaf compound-focal-length :tutorial "Touching Lenses")
	 (leaf wave-speed-refraction)
	 (leaf refraction-vacuum)
	 (leaf snells-law)
	 (leaf total-internal-reflection)
	 (leaf brewsters-law)
	 (leaf polarizer-intensity :bindings ((?fraction . 1)))
	 (leaf polarizer-intensity :bindings ((?fraction . 0)))
	 (leaf slit-interference)
	 (leaf frauenhofer-diffraction)
	 (leaf resolution-circular-aperture)
	 (leaf radiation-pressure)
	 )
 (group "Waves and Oscillations" 
	 (leaf wavenumber-lambda-wave)
	 (leaf frequency-of-wave)
	 (leaf period-of-wave)
	 (leaf speed-of-wave)
	 (leaf speed-equals-wave-speed)
	 (leaf wave-speed-string)
	 (leaf max-transverse-speed-wave)
	 (leaf max-transverse-abs-acceleration-wave)
	 (leaf spring-mass-oscillation)
	 (leaf pendulum-oscillation)
	 (leaf doppler-frequency)
	 (leaf harmonic-of)
	 (leaf beat-frequency)
	 (leaf energy-decay)
	 (leaf wave-speed-light)
	 )
 )
(group "Apply a Definition or Auxiliary Law"
 (group "Kinematics"
	 (group"Translational"
		 (leaf net-disp :bindings ((?axis . x)) :tutorial "Net Displacement")
		 (leaf net-disp :bindings ((?axis . y)) :tutorial "Net Displacement")
		 (leaf avg-velocity :bindings ((?axis . x)) :tutorial "Average velocity")
		 (leaf avg-velocity :bindings ((?axis . y)) :tutorial "Average velocity")
       (leaf lk-no-s :EqnFormat ("a(avg)_~a = (vf_~a - vi_~a)/t" 
				 (axis-name ?axis) (axis-name ?axis) 
				 (axis-name ?axis))
	     :bindings ((?axis . x)) :tutorial "Average acceleration")
       ;; alternative form of equation
       (leaf lk-no-s :EqnFormat ("a(avg)_~a = (vf_~a - vi_~a)/t" 
				 (axis-name ?axis) (axis-name ?axis) 
				 (axis-name ?axis))
	     :bindings ((?axis . y)) :tutorial "Average acceleration")
		 (leaf free-fall-accel :tutorial "Free fall acceleration")
		 (leaf std-constant-g :tutorial "Value of g near Earth")
		 (leaf period-circle :tutorial "Period Circular")
		)
	 (group "Rotational" 
		 (leaf ang-sdd :tutorial "Angular velocity")
		 (leaf rk-no-s :tutorial "Angular acceleration")
		 )
	 )
 (group	 "Newton's Laws" 
		 (group	 "Translational" 
			 (group"Force Laws" 
			 (leaf wt-law :tutorial "Weight Law")
			 (leaf kinetic-friction :tutorial "Kinetic Friction")
			 (leaf static-friction :tutorial "Static Friction max")
			 (leaf spring-law :tutorial "Hooke's Law")
			 (leaf tensions-equal :tutorial "Equal tensions at both ends")
			 (leaf thrust-force :tutorial "thrust force")
			 (leaf thrust-force-vector :bindings ((?axis . x)) :tutorial "thrust force")
			 (leaf thrust-force-vector :bindings ((?axis . y)) :tutorial "thrust force")
			 (leaf drag-force-turbulent :tutorial "drag force")
			 )
			 (group "Compound Bodies" 
				(leaf mass-compound :tutorial "Mass of a compound body")
				(leaf volume-compound)
				(leaf kine-compound :bindings ((?vec-type . velocity)) :tutorial "Kinematics of compound same as part")
				(leaf kine-compound :bindings ((?vec-type . acceleration)) :tutorial "Kinematics of compound same as part")
				(leaf force-compound :tutorial "Force on a compound body")
				)
			)
		 (group "Rotational" 
			(leaf net-torque-zc :tutorial "Net torque")
			(leaf torque :bindings ((?xyz . z)) :tutorial "Individual torque magnitude")
			(leaf mag-torque :tutorial "Individual torque magnitude")
			(group "Moment of Inertia" 
			 (leaf I-particle)
			 (leaf parallel-axis-theorem)
			 (leaf I-disk-cm :tutorial "disk about center of mass")
			 (leaf I-rod-cm :tutorial "Long thin rod about center of mass")
			 (leaf I-rod-end :tutorial "Long thin rod about end")
			 (leaf I-hoop-cm :tutorial "Hoop about center of mass")
			 (leaf I-rect-cm :tutorial "Rectangle about center of mass")
			 (leaf I-compound :tutorial "Compound body")
			 )
			)
		)
 (group "Work and Energy" 
	 (leaf work-nc :tutorial "Conservation of Energy")
	 (leaf mechanical-energy :tutorial "Conservation of Energy")
	 (leaf height-dy :tutorial "Height and Displacement")
	 )
 (group "Fluids" 
	 (leaf pressure-at-open-to-atmosphere :tutorial "Atmospheric Pressure")
	 (leaf std-constant-Pr0 :bindings 0 :tutorial "Atmospheric Pressure")
	 )
 )
(group "Calculate a vector component" 
 (group "Displacement" 
	 (leaf projection  :bindings ((?axis . x) (?vector . (displacement . ?rest))) :tutorial "Projection Equations")
	 (leaf projection :bindings ((?axis . y) (?vector . (displacement . ?rest))) :tutorial "Projection Equations")
	 )
 (group "Velocity" 
	 (leaf projection :bindings ((?axis . x) (?vector . (velocity . ?rest))) :tutorial "Projection Equations")
	 (leaf projection :bindings ((?axis . y) (?vector . (velocity . ?rest))) :tutorial "Projection Equations")
	 )
 (group "Acceleration" 
	 (leaf projection :bindings ((?axis . x) (?vector . (acceleration . ?rest))) :tutorial "Projection Equations")
	 (leaf projection :bindings ((?axis . y) (?vector . (acceleration . ?rest))) :tutorial "Projection Equations")
	 )
 (group "Force" 
	 (leaf projection :bindings ((?axis . x) (?vector . (force . ?rest))) :tutorial "Projection Equations")
	 (leaf projection :bindings ((?axis . y) (?vector . (force . ?rest))) :tutorial "Projection Equations")
	 )
 (group "Relative Position"
	 (leaf projection :bindings ((?axis . x) (?vector . (relative-position . ?rest))) :tutorial "Projection Equations")
	 (leaf projection :bindings ((?axis . y) (?vector . (relative-position . ?rest))) :tutorial "Projection Equations")
)
 (group "Momentum"
	 (leaf projection :bindings ((?axis . x) (?vector . (momentum . ?rest))) :tutorial "Projection Equations")
	 (leaf projection :bindings ((?axis . y) (?vector . (momentum . ?rest))) :tutorial "Projection Equations")
	 )
 )
(group "Use information specific to this problem"
 (leaf equals)
 (leaf angle-direction)
 (leaf displacement-distance)
 (leaf current-thru-what)
 (leaf num-forces)
 (leaf given-fraction)
 (leaf opposite-relative-position)
 (leaf relative-position-displacement :bindings ((?axis . x)))
 (leaf relative-position-displacement :bindings ((?axis . y)))
 (leaf sum-times :tutorial "Sum of times")
 (leaf sum-distances)
 (leaf sum-distance)
 (leaf pyth-thm :tutorial "Pythagorean Theorem")
 (leaf unit-vector-mag) 
 (leaf connected-accels :tutorial "Equal accelerations")
 (leaf connected-velocities :tutorial "Equal velocities")
 (leaf complimentary-angles)
 (leaf supplementary-angles)
 (leaf right-triangle-tangent)
 (leaf tensions-equal :tutorial "Equal tensions at both ends")
 (leaf vector-magnitude :tutorial "Pythagorean Theorem")
 (leaf rdiff :bindings ((?axis . x)))
 (leaf rdiff :bindings ((?axis . y)))
 (leaf area-of-rectangle)
 (leaf area-of-rectangle-change)
 (leaf area-of-circle)
 (leaf circumference-of-circle-r)
 (leaf circumference-of-circle-d)
 (leaf volume-of-cylinder)
 (leaf mass-per-length-eqn)
 (leaf turns-per-length-definition)
 (leaf average-rate-of-change)
 )
))


;;;          Generate file KB/principles.tsv

(defun principles-file ()
  "construct file solutions/principles.tsv"
  (let ((str (open (merge-pathnames  "solutions/principles.tsv" *Andes-Path*)
		   :direction :output :if-exists :supersede
		   ;; The workbench uses an older windows-specific 
		   ;; character encoding
	         :external-format #+sbcl :windows-1252 #+allegro :1252)))
    (dolist (p *principle-tree*) (principle-branch-print str p))
    (close str)))

(defun principle-branch-print (str p)
  "prints a group in KB/principles.tsv"
  (cond ((eq (car p) 'group)
	 ;; principles.tsv file format is 4 tab-separated columns
	 (format str "GROUP~C~A~C~C~%" #\tab (cadr p) #\tab #\tab)
	 (dolist (pp (cddr p)) (principle-branch-print str pp))
	 (format str "END_GROUP~C~A~C~C~%"  #\tab (cadr p) #\tab #\tab))
	((eq (car p) 'leaf)
	 (apply #'principle-leaf-print (cons str (cdr p))))))

;; keywords :short-name and :EqnFormat override definitions in Ontology
(defun principle-leaf-print (str class &key tutorial (bindings no-bindings)
					EqnFormat short-name) 
  "prints a principle in KB/principles.tsv"
  (let ((pc (lookup-psmclass-name class)))
    (format str "LEAF~C~A    ~A~C~(~A~)~C~@[~A~]~%" #\tab 
	    (eval-print-spec (or EqnFormat (psmclass-EqnFormat pc)) bindings)
	    (eval-print-spec (or short-name (psmclass-short-name pc)) bindings)
	    #\tab
	    (if (eq bindings no-bindings) (psmclass-name pc)
	      ;; if bindings have been supplied, construct list
	      ;; turn off pretty-print to prevent line breaks
	      (write-to-string (list (psmclass-name pc) bindings) :pretty nil))
	    #\tab
	    tutorial)))


;;; 
;;;            List of Andes distribution homework sets
;;;
;; This list was generated by applying the following perl script
;; to solutions/*.aps & solutions/index.html.
;;
;; cd solutions; perl -w -n -e 'if(m/"(.*?\.aps)">(.*?)</){open F,"< $1" or next;my @y=<F>;close F;shift @y;shift @y;chomp(@y);print "(\"$2\" (@y))\n";}' solutions/index.html
;;
(setf *sets* '(("Vectors" (vec1a vec1b vec1c vec1d vec1e vec1f vec2a vec2b vec2c vec2d vec2e vec2f vec3a vec3b-DEMO.wmv vec3b vec3c vec3d vec4a vec4b vec4c vec4d vec5a vec5b vec5c vec5d vec6a vec6b vec6c vec6d vec7a vec8a vec8b vec8c vec9 relvel1a-DEMO.wmv relvel1a relvel2a relvel3a mot1 mot2 mot3 mot4 vec20 vec21 ))
("Kinematics Graphs" (kgraph2 kgraph3 kgraph4 kgraph5 kgraph5b kgraph5c kgraph5d kgraph5e kgraph5f kgraph6 kgraph7 kgraph8 kgraph8b kgraph8c kgraph8d kgraph8e kgraph8f kgraph8g kgraph9 kgraph9b kgraph10 kgraph10b kgraph11 kgraph12 kgraph13 kgraph14 kgraph16 kgraph17 kgraph18 kgraph19 kgraph20 kgraph21 kgraph22 kgraph30 kgraph31 ))
("Translational Kinematics" (kt1a kt1b kt2a kt2b kt3a kt3b kt4a-DEMO.wmv kt4a kt4b kt4c kt5a kt6a kt6b kt7a kt7b kt8a kt8b kt9a kt9b kt9c kt10a kt10c kt11a kt11b kt11c kt12a-DEMO.wmv kt12a kt12b kt12c kt13a-DEMO.wmv kt13a kt13b kt13c kt13d kt14a kt14b kt20 kt21))
("Free Body Diagrams" (fbd1b fbd2a fbd3a-DEMO.wmv fbd3a fbd4a fbd5a fbd6a fbd8 fbd9 fbd10 fbd11 fbd12 fbd13 fbd14 fbd20 fbd21))
("Statics" (s1b s1c-DEMO.wmv s1c s1d s1e s1f s2a s2b s2c s2d s2e s3a s3b s3c-DEMO.wmv s3c s4a s4b s5a s6a s7a s7b s8a s9a-DEMO.wmv s9a s10a s11a s11b s12a s13 s14 s15 s16 s17 s30 s31 ))
("Translational Dynamics" (dt1a dt1b dt1c dt2a-DEMO.wmv dt2a dt3a dt3b dt3c dt4a-DEMO.wmv dt4a dt4b dt5a dt6a-DEMO.wmv dt6a dt6b dt6c dt7a dt7b dt8a dt9a dt9b dt10a dt11a dt11b dt12a dt13a dt13b dt14a dt14b dt16 dt17 dt18 dt19 nl1 nl2 nl3 dt30 dt31 dt32))
("Circular Motion" (rots1b rots1c rots2a rots3a-DEMO.wmv rots3a rots4a rots5a rots6a rots6b rots6c rots7a rots8a rots8b rots20 rots21 rots22 ))
("Work and Energy" (weq2 weq4 weq5 e1a e1b e1c e2a e2b e2c-DEMO.wmv e2c e3a e3b e3c e3d e4a e4b e4c-DEMO.wmv e4c e5a e5b e6a e7a e7b e8a e8b e9a e9b e10a e11a we1a we2a we3a we4a we5 we6 we8 we9 we10 egrav1 egrav2 egrav3 egrav4 egrav5 we20 we21 ))
("Power" (pow1a pow1b pow2a pow3a pow4a pow4b pow5a pow5b pow5c pow5d pow6a pow10 pow11 pow12 ))
("Linear Momentum" (mom2 mom3 mom1a-DEMO.wmv lmom1a lmom1b lmom2a lmom2b lmom3a lmom3b lmom3c lmom4a lmom5 lmom5b lmom5c lmom6 lmom7 lmom8 lmom9 lmom10 lmom11 pgraph1 pgraph2 pgraph3 imp1 imp2 imp3a imp3b imp3c imp4 cm1 cm2 cm3 roc1 roc2 roc3 roc4 roc5 roc6 lmom20 lmom21))
("Rotational Kinematics" (kr9 kr1a-DEMO.wmv kr1a kr1b kr1c kr2a kr2b-DEMO.wmv kr2b kr3a kr3b kr3c kr4a kr4b kr5a kr6a kr7a kr10 kr20 kr21   ))
("Angular Momentum" (momr1a momr1b momr2a momr2b momr3a momr4a momr10 momr11))
("Rotational Dynamics" (dr2a-DEMO.wmv dr2a dr2b dr3a dr4a dr5a dr6a dr6b dr7a dr8a dr20 dr21 erot2 erot3 erot4 erot5 erot6 grav1 grav2 grav3 grav4 grav5 grav6))
("Fluids" (fluids2 fluids3 fluids4 fluids5 fluids6 fluids7 fluids8 fluids9 fluids11 fluids12 fluids13 fluids14 fluids15 fluids20 fluids21  ))
("Oscillations" (osc2 osc3 osc4 osc5 osc6 osc7 osc8 osc20 osc21 ))
("Waves" (wave2 wave3 wave4 wave5 wave6 wave8 wave9 wave10 wave11 wave12 wave13 wave14 wave15 wave16 wave17 wave18 wave24 wave30 wave31))
("Review" (rr2 rr3 rr4  rr5 rr6 rr7 rr8 rr9 rr10 rr11 rr12 rr13 rr14 rr15 rr16 rr17 rr18 rr19 rr20))
("Electric Field" (charge1b charge2 coul1a coul1b coul1c coul2a coul2b coul2c coul3 ediag1 ediag2 ediag3 ediag4 efield1a efield1b efield1c efield1d efield1e efield2 efield3 efield4a efield4b efield5 efield6 for1a for1b-DEMO.wmv for1b for1c for2a for2b for4a for4b for5 for7a for7b for8a for8b for9a for9b for10a for10b for11a for11b for11c elec1a elec1b elec1d elec1e elec2 elec3b elec4b elec5b elec6b elec7 elec8 elec9 elec10 elec11 gauss1 gauss3 gauss4 gauss5 gauss6 gauss8 gauss9 gauss10 gauss11 dip1a dip1b))
("Electric Potential" (epot1b epot1c epot2 epot3 epot4 epot5 epot6 epot7 epot8 pot1a pot1b pot2a pot2b pot2c pot3a pot3b pot4 pot5-DEMO.wmv pot5 pot6 pot7 pot8))
("Resistance" (eqres1b eqres1c-DEMO.wmv eqres1c eqres1d eqres1e eqres2a eqres2b eqres3a eqres3b eqres4a eqres4b eqres5a eqres6a eqres7a eqres7b eqres8a eqres8b))
("Capacitance" (eqcap1b-DEMO.wmv eqcap1b eqcap1c eqcap1d eqcap2a eqcap2b eqcap3a eqcap3b eqcap4a eqcap4b eqcap5a eqcap6a cap1a cap1b cap2a cap2b cap3a cap4a cap5a cap6a cap6b cap9a cap9b))
("DC Circuits" (kir1b kir1c kir1d kir1e kir1f kir2a-DEMO.wmv kir2a kir3a kir3b kir3c kir4a kir5a kir7a epow1 epow2 epow3 epow4 rc1a rc1b-DEMO.wmv rc1b rc1c rc2a rc3a rc3b rc4a rc4b rc5a rc6a rc7a rc7b rc8 rc9  ))
("Magnetic Field" (mag1b mag1c mag2a-DEMO.wmv mag2a mag2b mag3a mag3b mag4a mag5a mag5b mag5c mag5d mag5e mag5f magtor1a magtor1b magtor1c magtor1d magdip1 magdip2 magdip3 magdip4 mag6a mag6b mag6c mag7 mag8a mag8b mag9 mag10 mag11 mag12 ))
("Electromagnetic Induction" (fara1b fara2a-DEMO.wmv fara2a fara2b fara3a fara3b fara4a fara4b fara5a fara5b fara5c fara6a fara6b fara7a fara7b fara7c fara7d fara8a fara8b fara8c fara9 fara10a fara10b fara11a fara11b amp1))
("Inductance " (ind1a ind1b ind1c ind2a ind3a ind3b ind3c ind4 LR1a-DEMO.wmv LR1a LR1b LR1c LR1d LR2a LR2b LR3a LR3b LC1a LC2a LC2b LRC1a LRC2a))
("Electromagnetic Waves" (wave19 emwave3a emwave4 emwave5 ))
("Optics" (mirror2 mirror3 mirror4-DEMO.wmv mirror4 lens1a lens1b lens2a lens2b lens3a lens3b lens4a lens4b lens5a lens5b ref1 ref2a-DEMO.wmv ref2a ref2b ref2c ref3a ref3b ref4a ref4b ref5a ref5b ref6 int1a int1b int1c int1d int2a int2b))))

;;
;; Median completion times gotten from running LogProcessing/onelog.pl
;; over USNA from Fall 2005 to Spring 2008
;; 
(setf *times-scores* '(
(amp1 236 92)
(cap1a 584 64)
(cap1b 328.5 89.5)
(cap2a 380 78)
(cap2b 202 94)
(cap3a 506.5 68)
(cap4a 526 65.5)
(cap5a 593 78.5)
(cap6a 222 98)
(cap6b 145 99)
(cap9a 29 94)
(cap9b 21.5 92)
(charge1a 658 75)
(charge1b 198 86)
(charge2 31 100)
(cm1 640.5 57.5)
(cm2 526.5 56.5)
(cm3 512 74)
(coul1a 643.5 77)
(coul1b 524 82)
(coul1c 514 57)
(coul2a 1379.5 39)
(coul2b 1204.5 73.5)
(coul2c 469 45.5)
(coul3 1077 43)
(dip1a 947.5 60)
(dip1b 954 43.5)
(dq1 32 95)
(dr1a 120 95)
(dr2a 663 75.5)
(dr2b 491.5 83)
(dr3a 789 83)
(dr4a 714 78)
(dr5a 945 61)
(dr6a 417.5 81.5)
(dr6b 342 81)
(dr7a 1093.5 38)
(dr8a 813.5 72)
(dt10a 2496 37)
(dt11a 1668 53)
(dt11b 1525.5 33)
(dt12a 595 77)
(dt13a 338 85)
(dt13b 496.5 87.5)
(dt14a 242 94.5)
(dt14b 302 93)
(dt1a 572 72)
(dt1b 372 76)
(dt1c 534 78)
(dt2a 419 94)
(dt3a 418 88)
(dt3b 428.5 54)
(dt4a 564.5 84.5)
(dt4b 418 91)
(dt5a 466 82)
(dt6a 1260.5 58)
(dt6c 1468 93)
(dt7a 285 99)
(dt7b 540.5 92)
(dt7bplan 218 100)
(dt8a 299.5 64.5)
(dt9a 719 83)
(e10a 507 89)
(e11a 510 92)
(e1a 238 98)
(e1b 292 96)
(e1c 271 96)
(e2a 489 88)
(e2b 327 98)
(e2c 750 95)
(e3a 292 97)
(e4a 673 77)
(e4b 379 95)
(e4c 263.5 84.5)
(e5a 304 94)
(e5b 236 100)
(e6a 238 99)
(e7a 647 88)
(e7aplan 313 100)
(e7b 327 91)
(e8a 828.5 79.5)
(e8b 671.5 87)
(e9a 636.5 87.5)
(e9b 733 84)
(efield1a 239.5 94)
(efield1b 125 98)
(efield1c 181 92)
(efield1d 182 93)
(efield1e 256 86)
(efield2 36 100)
(efield3 41 96)
(efield4a 151 95)
(efield4b 124 94)
(elec1a 612 82.5)
(elec1b 158.5 42.5)
(elec2 369 30)
(elec3b 901 59)
(elec4b 485.5 89)
(elec5b 439 88)
(elec6b 1086.5 39)
(emwave1 162 93)
(emwave2a 306.5 84.5)
(emwave3a 330.5 83.5)
(emwave4 306 84)
(emwave5 351 75)
(epot1a 59.5 94)
(epot1b 37 92)
(epot1c 50 90)
(epot2 27 94)
(epow1 243 83)
(epow2 293 88)
(epow3 382 82)
(epow4 368 89)
(eqcap1a 182 90)
(eqcap1b 67 98)
(eqcap1c 161 96)
(eqcap1d 114.5 87)
(eqcap2a 130 60.5)
(eqcap2b 134.5 87.5)
(eqcap3a 162.5 98)
(eqcap3b 83 69)
(eqcap4a 209 92)
(eqcap4b 131 70)
(eqcap5a 171 95)
(eqcap6a 300 95)
(eqres1a 143 95)
(eqres1b 78 100)
(eqres1c 95 99)
(eqres1d 143 71)
(eqres1e 122 100)
(eqres2a 78 90)
(eqres2b 102 75)
(eqres3a 89.5 70)
(eqres3b 68 59)
(eqres4a 86 80)
(eqres4b 174.5 66.5)
(eqres5a 149.5 99)
(eqres6a 440 81)
(eqres7a 54 93)
(eqres7b 26.5 97)
(eqres8a 35 94)
(eqres8b 31 93)
(erot2 572 83)
(erot3 1366.5 89.5)
(erot4 46 89)
(fara10a 239 98)
(fara10b 188.5 99)
(fara11a 667 49)
(fara11b 433 71.5)
(fara1a 78.5 93)
(fara1b 34 97)
(fara2a 28 97)
(fara2b 24 97)
(fara3a 38 94)
(fara3b 21.5 97)
(fara4a 23 97)
(fara4b 18 97)
(fara5a 44 94)
(fara5b 26 97)
(fara5c 24 94)
(fara6a 44.5 94)
(fara6b 26 94)
(fara7a 40 93)
(fara7b 19 96)
(fara7c 19 96)
(fara7d 17.5 96)
(fara8a 44.5 96)
(fara8b 29 92)
(fara8c 23 96)
(fara9 721 48)
(fbd1a 371 79)
(fbd1b 132.5 94)
(fbd2a 180 93)
(fbd3a 155 95)
(fbd4a 157.5 86.5)
(fbd5a 116.5 97)
(fbd6a 269 88)
(fbd8 73 100)
(fbd9 99 83)
(fluids1 432 89)
(fluids11 438.5 84)
(fluids12 290 94)
(fluids13 637 85)
(fluids14 616.5 75)
(fluids15 493 69)
(fluids2 418.5 93)
(fluids3 243 99)
(fluids4 191 98)
(fluids5 501 88)
(fluids6 416.5 95.5)
(fluids7 750 58.5)
(fluids8 907.5 49.5)
(fluids9 618 78)
(for1 382.5 99)
(for10 485 92.5)
(for10a 506 63)
(for10b 706 78)
(for11a 1063.5 48.5)
(for11b 853 58)
(for11c 669 69)
(for1a 547 92.5)
(for1b 283.5 78.5)
(for1c 797 85)
(for2 143 99)
(for2a 213 92)
(for2b 193 99)
(for4 277.5 99)
(for4a 284 92)
(for4b 289 97)
(for5 223 91)
(for7 400 96)
(for7a 319.5 48.5)
(for7b 205 96)
(for8 370 97.5)
(for8a 578 65)
(for8b 412 90)
(for9 759.5 88.5)
(for9a 884 76)
(for9b 1076 82)
(gauss1 59 94)
(gauss10 602 78)
(gauss11 978 56)
(gauss3 578 53)
(gauss4 42 96)
(gauss5 241 83)
(gauss6 552 96)
(gauss8 617 53.5)
(gauss9 856 80)
(imp1 578 77)
(imp2 525 79)
(imp3a 490 94)
(imp3b 362 76)
(imp3c 447.5 90.5)
(ind1a 188 94)
(ind1b 104 98)
(ind1c 110 99)
(ind2a 77 98)
(ind3a 416 90)
(ind3b 321 90.5)
(ind3c 167 97)
(ind4 419 79)
(int1a 657 62)
(int1b 307 89)
(int1c 171 94)
(int1d 51 95)
(int2a 356.5 84)
(int2b 127 96)
(kgraph1 73 100)
(kgraph10 264 42)
(kgraph11 26 91)
(kgraph12 318 42)
(kgraph13 35 94)
(kgraph14 98 97)
(kgraph16 303 35)
(kgraph17 153 66)
(kgraph18 17 50)
(kgraph19 33 20)
(kgraph2 37 100)
(kgraph21 523 44)
(kgraph3 51 97)
(kgraph4 48 97)
(kgraph5 39 100)
(kgraph6 49 97)
(kgraph7 29.5 100)
(kgraph8 37 92)
(kgraph9 260 84)
(kir1a 225 97)
(kir1b 60 91)
(kir2a 498 74)
(kir3a 169 90)
(kir3b 121 44)
(kir3c 27 19)
(kir4a 336.5 86.5)
(kir5a 698 85.5)
(kir7a 432 72.5)
(kr1a 323 83)
(kr1b 186 88)
(kr2a 258 89)
(kr2b 261 85)
(kr3a 289 84)
(kr3b 412.5 88.5)
(kr4a 502 83)
(kr5a 407 83)
(kr6a 586.5 75.5)
(kr7a 281 90)
(kt10a 863 82)
(kt10c 529 93)
(kt11a 504 98)
(kt11b 634 89)
(kt12a 646 92)
(kt12b 464 39)
(kt12c 1000 59)
(kt13a 469.5 50.5)
(kt13b 526.5 53.5)
(kt13c 793 83)
(kt14a 396 79)
(kt14b 1194 38)
(kt1a 261 97)
(kt1b 176 98.5)
(kt2a 829 75)
(kt2b 1010 4)
(kt3a 1148 83)
(kt3b 753.5 85)
(kt4a 348 93)
(kt4b 1171 66)
(kt5a 781 80)
(kt6a 307 95)
(kt6b 565 93)
(kt7a 1327 75)
(kt7b 63 29)
(kt8a 981 68)
(kt8b 742 67)
(kt9a 827 85)
(kt9b 524 93)
(lc1a 400 84)
(lc2a 242 88)
(lc2b 145 97)
(lens1a 98 98)
(lens1b 88 98)
(lens2a 99.5 99)
(lens2b 123 98)
(lens3a 416 89)
(lens3b 282.5 96)
(lens4a 255.5 89)
(lens4b 194 83)
(lens5a 502 73)
(lens5b 280 91)
(lmom1a 457 69)
(lmom1b 378 94)
(lmom2a 502 80)
(lmom2b 1090 74)
(lmom3a 1397 68)
(lmom4a 828 86)
(lmom4aplan 60 100)
(lmom5 1277 57)
(lmom6 1379 67)
(lmom7 1032 64)
(lr1a 385 77)
(lr1b 280 73)
(lr1c 561.5 72)
(lr1d 237.5 79.5)
(lr2a 270.5 84.5)
(lr2b 138 59)
(lr3a 361 74)
(lr3b 400 72)
(lrc1a 470 81)
(lrc2a 629 69)
(mag10 1199 59)
(mag11 589.5 57.5)
(mag12 464 69)
(mag1a 547.5 77)
(mag1b 222 94)
(mag1c 513 83)
(mag2a 364 83)
(mag2b 237 92)
(mag3a 587 89)
(mag3b 552 87)
(mag4a 564.5 61.5)
(mag5a 519 73)
(mag5b 300.5 83)
(mag6a 370 93)
(mag6b 200 86)
(mag6c 128 92)
(mag7 406 71)
(mag8a 241 88.5)
(mag8b 198 93)
(mag9 1155 69)
(magdip1 492 79.5)
(magdip2 649 69)
(magdip3 381 92)
(magdip4 722 61)
(magtor1a 176 87)
(magtor1b 60 100)
(magtor1c 63 94)
(magtor1d 49 95)
(mirror1 167.5 92)
(mirror2 210.5 94)
(mirror3 146 92)
(mirror4 150.5 97)
(momr1a 349 86)
(momr1b 215.5 83)
(momr2a 279 92)
(momr2b 226 93)
(momr3a 360 76)
(momr4a 551.5 75)
(mot1 76 92)
(mot2 33 100)
(mot3 31 100)
(mot4 1027 92)
(osc1 276.5 79)
(osc2 201 97)
(osc3 166 97)
(osc4 669.5 51)
(osc5 408 81)
(osc6 156.5 98)
(osc7 235.5 96.5)
(osc8 256 98)
(pgraph1 66 92)
(pgraph2 25.5 93)
(pgraph3 24 92)
(posttest 1728 100)
(pot1a 579 78)
(pot1b 589.5 89.5)
(pot2a 786 79)
(pot2b 471.5 77.5)
(pot2c 689 71.5)
(pot3a 683 76.5)
(pot3b 624.5 80)
(pot4 613 83)
(pot5 550 64)
(pot6 428.5 77.5)
(pot7 523.5 54)
(pot8 277 78)
(pow1a 373 94)
(pow1b 372.5 92)
(pow2a 368 84)
(pow3a 477 82)
(pow4a 869 68)
(pow4b 930 73)
(pow5a 1205.5 57)
(pow5aplan 279.5 100)
(pow5b 695 79)
(pow5c 493 96.5)
(pow5d 723.5 83)
(pow6a 1032 63)
(pretest 855 100)
(rc1a 304 83.5)
(rc1b 262.5 87)
(rc1c 270.5 95)
(rc2a 174.5 94)
(rc3a 214 90)
(rc3b 357 90)
(rc4a 287 73)
(rc4b 250.5 16)
(rc5a 331 79)
(rc6a 282.5 80)
(rc7a 222 84)
(rc7b 435 80.5)
(rc9 1 19)
(ref1 389 84)
(ref2a 399 75)
(ref2aa 33 31)
(ref2b 248 89)
(ref2c 380 81.5)
(ref3a 529 60)
(ref3b 235 87.5)
(ref4a 347 84)
(ref4b 178.5 95)
(ref5a 310 80)
(ref5b 238 86.5)
(ref6 289 71)
(relvel1a 989 81)
(relvel2a 410 85)
(relvel3a 642.5 88.5)
(roc1 277.5 92)
(roc2 383 84)
(roc3 388 73)
(roc4 312 85)
(roc5 800 53)
(roc6 426 93)
(rots1a 514 89)
(rots1b 549 49)
(rots1c 1807.5 95.5)
(rots2a 438 98)
(rots3a 455 91)
(rots4a 395 93)
(rots5a 358 93)
(rots6a 474 84)
(rots6b 493 98.5)
(rots6c 634 85.5)
(rots7a 572 88)
(rots8a 685 73)
(rots8b 428.5 77.5)
(s10a 381 100)
(s11a 456.5 83)
(s11b 339.5 76)
(s12a 428.5 62)
(s1a 346 64)
(s1b 427 81)
(s1c 178.5 99)
(s1d 136 100)
(s1e 304.5 92.5)
(s1f 343 88)
(s2a 128.5 82)
(s2b 771 77)
(s2c 483 78.5)
(s2d 971 54)
(s2e 830 83.5)
(s3a 400 98)
(s3c 639.5 88)
(s4a 223 93)
(s4b 566 90)
(s5a 623.5 98.5)
(s6a 1308 67)
(s6aplan 295 100)
(s7a 895 84)
(s7b 234 77)
(s8a 706 78)
(s9a 502 87)
(vec1a 698 95.5)
(vec1b 571 95)
(vec1c 565 97)
(vec1d 318.5 97)
(vec2a 225 99)
(vec2b 250.5 97)
(vec2c 141 99)
(vec2d 163.5 100)
(vec3a 558 97)
(vec3b 802.5 92)
(vec3c 490 97)
(vec4a 523 96)
(vec4b 420 98)
(vec4c 300 97)
(vec4d 299.5 99)
(vec5a 516.5 98)
(vec5b 386 98)
(vec5c 327 99)
(vec5d 374 99)
(vec6a 937.5 89)
(vec6b 395 97)
(vec6c 449 98)
(vec6d 439 98)
(vec7a 581 89.5)
(vec8a 510 98)
(vec8b 552 98)
(vec8c 378 99)
(vec9 97 96)
(wave1 493 91.5)
(wave10 477 64)
(wave11 363 89)
(wave12 332 66)
(wave13 261 98)
(wave14 462.5 75.5)
(wave15 418 84)
(wave16 367.5 89)
(wave17 409 84.5)
(wave18 174 96)
(wave19 349 74)
(wave2 85 99)
(wave3 76 99)
(wave4 364.5 89)
(wave5 308 90)
(wave6 356 85)
(wave8 570 79)
(wave9 402 74)
(we1a 111 99)
(we2a 304 99)
(we3a 384 99)
(we4a 531 83)
(we5 317.5 77.5)
(we6 779.5 89.5)))

(defun sets-json-file ()
  "construct file Documentation/sets.json"
  (let ((Stream  (open 
		  (merge-pathnames  "Documentation/sets.json" *Andes-Path*)
		  :direction :output :if-exists :supersede)))
    ;;  Assume stream has UTF-8 encoding (default for sbcl)
    ;;  Should test this is actually true or change the charset to match
    ;;  the actual character code being used by the stream
    ;;  something like:
    (when (streamp Stream) 
      #+sbcl (unless (eq (stream-external-format Stream) ':utf-8)
	       (error "Wrong character code ~A, should be UTF-8" 
		      (stream-external-format Stream))))
    (format Stream 
	    (strcat "{~%"
		    "  identifier: 'id',~%"
		    "  label: 'label',~%"
		    "  items: [~%"
		    ))
    
    (dolist (set *sets*)
      (let ((probs (remove nil (mapcar #'get-problem (second set)))))
	(format t "set ~a~%" (car set))
	(format stream " {id: '~A', label: '~A', items: [~%" (car set) (car set))
	(problem-lines-json stream probs)
	(format stream "]}~@[,~]~%" (not (eq set (car (last *sets*)))))))
    (format Stream (strcat
		    "  ]~%"
		    "}~%"))
    (when (streamp stream) (close stream))))

(setf jsonc 0)

;;;
;;;  See Bug #1475
;;;   script to fix the math notation
;;;   perl -pi.orig -e 's/& /&amp; /g; s/([^_])_([xyz])/$1<sub>$2<\/sub>/g; s/\^(.)/<sup>$1<\/sup>/g; s/\*/ /g;' principles.json
;;;   script to fix the special characters
;;;   perl -pi.orig -e 's/\$w/&omega;/g; s/\$p/&pi;/g; s/\$S/&Sigma/g; s/\$q/&theta;/g; s/\$l/&lambda;/g; s/\$a/&alpha;/g; s/\$r/&rho;/g; s/\$F/&Phi;/g; s/\$e/&epsilon;/g; s/\$m/&mu;/g; s/\$t/&tau;/g; s/\$b/&beta;/g;' principles.json


(defun principles-json-file ()
  "construct file Documentation/principles.json"
  (let ((Stream  (open 
		 (merge-pathnames  "Documentation/principles.json" *Andes-Path*)
		   :direction :output :if-exists :supersede)))
  (andes-init)
  (setf jsonc 0)
  ;;  Assume stream has UTF-8 encoding (default for sbcl)
  ;;  Should test this is actually true or change the charset to match
  ;;  the actual character code being used by the stream
  ;;  something like:
  (when (streamp Stream) 
    #+sbcl (unless (eq (stream-external-format Stream) ':utf-8)
	     (error "Wrong character code ~A, should be UTF-8" 
		    (stream-external-format Stream))))
    (format Stream 
	    (strcat "{~%"
		    "  identifier: 'id',~%"
		    "  label: 'label',~%"
		    "  items: [~%"
		    ))

    (dolist (p *principle-tree*) (principle-branch-print-json stream p)
	    (format stream "~@[,~]~%" 
		    (not (eq p (car (last *principle-tree*))))))

    (format Stream (strcat
		    "  ]~%"
		    "}~%"))
    (when (streamp stream) (close stream))))

(defun principle-branch-print-json (stream p)
  "prints a group in Documentation/principles.json"
  (cond ((eq (car p) 'group)
	(format stream "  {id: \"~A~A\", label: \"~A\", items: [~%" 
		(cadr p) (incf jsonc) (cadr p))
	 (dolist (pp (cddr p)) 
	   (principle-branch-print-json stream pp)
		 (format stream "~@[,~]~%" (not (eq pp (car (last (cddr p)))))))
	 (format stream "  ]}"))
	((eq (car p) 'leaf)
	(apply #'principle-leaf-print-json (cons stream (cdr p)))
	 (incf jsonc)
)))

;; keywords :short-name and :EqnFormat override definitions in Ontology
(defun principle-leaf-print-json (stream class &key tutorial (bindings no-bindings)
				  EqnFormat short-name) 
  (format t "print leaf ~A~%" class)
  (let ((cont nil) 
	(pc (lookup-psmclass-name class)))
 	(format stream "    {id: '~A~A', label: \"~A    ~A\", items: [~%" 
		(subst-bindings bindings (psmclass-name pc))
		(incf jsonc)
		(eval-print-spec (or EqnFormat (psmclass-EqnFormat pc)) bindings)
	    (eval-print-spec (or short-name (psmclass-short-name pc)) bindings))
    (dolist (set *sets*)
      (let ((probs (remove-if-not
		    #'(lambda (Prob)
			(when Prob 
			  (unless (problem-graph Prob)
			    (read-problem-info (string (problem-name prob)))
			    (when *cp* (setf (problem-graph Prob) 
					     (problem-graph *cp*))))
			  (some #'(lambda (enode)
				    (unify (psmclass-form pc)
					   (enode-id enode) bindings)) 
				(second (problem-graph Prob)))))
		    (mapcar #'get-problem (second set)))))
	(when probs 
	  (when cont (format stream ",~%"))
	  (setf cont t)
	  (problem-lines-json stream probs (car set) jsonc))))
    (format stream "    ]}")))

(defun problem-lines-json (stream probs &optional set id)
  (dolist (prob probs)
    (format stream "        {id: '~A~@[~A~]', expand: \"~(~A~).html\", ~@[graphic: \"~A\", ~] ~@[set: \"~A\",~] label: \"~(~A~): ~@[~,1Fm~] ~@[~A%~]\"}~@[,~]~%"
	    (problem-name prob) id
	    (problem-name prob)
	    (problem-graphic prob)
	    set
	    (problem-name prob)
	    (when (find (problem-name prob) *times-scores* :key #'car)
	      (/ (second (find (problem-name prob) *times-scores* 
			       :key #'car)) 60))
	    (third (find (problem-name prob) *times-scores* :key #'car))
	    (not (eq prob (car (last probs)))))))

;; Use the above perl script to fix the math formatting after the files
;; are generated.
;; (problem-html-files #P"/Users/bvds/problems/")
;; cp solutions/*.gif ~/problems/
;; cp solutions/*.jpg ~/problems/

(defun problem-html-files (&optional (path *andes-path*))
  "construct html files for all problems"
(dolist (prob (listprobs))
  (when (problem-graphic prob) (format t "~A~%" (problem-graphic prob)))
  (let ((*print-pretty* NIL) ;disble line breaks
	(stream (open (merge-pathnames 
	         (format nil "~(~A~).html" (problem-name prob)) path)
		      :direction :output :if-exists :supersede)))
    
  ;;  Assume stream has UTF-8 encoding (default for sbcl)
  ;;  Should test this is actually true or change the charset to match
  ;;  the actual character code being used by the stream
  ;;  something like:
  (when (streamp Stream) 
    #+sbcl (unless (eq (stream-external-format Stream) ':utf-8)
             (error "Wrong character code ~A, should be UTF-8" 
                    (stream-external-format Stream))))
  (format Stream 
          (strcat
           "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">~%"
           "<html> <head>~%"
           ;;      "   <meta http-equiv=\"Content-Type\" content=\"text/html; "
	   ;;     "charset=iso-8859-1\">~%"
           "   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UT
F-8\">~%"
           "<title>Problem ~(~A~)</title>~%"
           "</head>~%"
           "<body>~%") (problem-name prob))
  (format stream "~{    ~A<br>~%~}" (problem-statement prob))
  (format stream (strcat "</body>~%" 
			 "</html>~%"))
  (when (streamp stream) (close stream)))))
